package com.soft.comprehensiveexercise.game;

import java.util.Random;
import java.util.Scanner;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/15
 */
public class FingerGuessGame {

    // 游戏角色
    public static String[] myRoleArray = {"沸羊羊", "暖羊羊", "美羊羊"};
    public static String[] computerRoleArray = {"喜羊羊", "慢羊羊", "懒羊羊"};

    // 猜拳选项
    public static String[] choice = {"石头", "剪刀", "布"};

    // 游戏状态
    public static String myRole;
    public static String computerRole;
    public static int myScore = 0;
    public static int computerScore = 0;
    public static boolean gameRunning = true;
    public static String isContinue = "开始游戏? (y/n)";
    public static int playCount = 0;
    public static int equalCount = 0;

    public static Scanner sc = new Scanner(System.in);
    public static Random random = new Random();

    public static void main(String[] args) {
        showWelcome();
        selectMyRole();
        selectComputerRole();

        while (gameRunning) {
            askStart();
            if(gameRunning){
                playRound();
            }

        }

        if(playCount == 0){
            System.out.println("还没开始玩呢");
        }else {
            showExitMessage();
        }
    }

    // 显示欢迎界面
    public static void showWelcome() {
        System.out.println("---------欢迎进入羊村游戏世界---------");
        System.out.println("\t\t*****************");
        System.out.println("\t\t **  游戏开始  **");
        System.out.println("\t\t*****************");
        System.out.println("出拳规则：1.石头 2.剪子 3.布");
    }

    // 选择玩家角色
    public static void selectMyRole() {
        System.out.print("请选择你的角色(1.沸羊羊 2.暖羊羊 3.美羊羊)：");
        int myNumber = sc.nextInt();
        if(myNumber < 1 || myNumber > 3){
            System.out.println("输入无效，请输入1/2/3");
            selectMyRole();
        }else {
            myRole = myRoleArray[myNumber-1];
        }


    }

    // 选择对手角色
    public static void selectComputerRole() {
        System.out.print("请选择对手角色(1.喜羊羊 2.慢羊羊 3.懒羊羊)：");
        int computerNumber = sc.nextInt();
        if(computerNumber < 1 || computerNumber > 3){
            System.out.println("输入无效，请输入1/2/3");
            selectComputerRole();
        }else {
            computerRole = computerRoleArray[computerNumber -1];
        }

    }

    // 进行一局游戏
    public static void playRound() {
        System.out.print("请出拳: 1.石头 2.剪子 3.布 ");
        int myChoice = sc.nextInt();
        int computerChoice = random.nextInt(3)+1;

        if(myChoice < 1 || myChoice > 3){
            System.out.println("输入无效，请输入1/2/3");
            playRound();
        }else {
            System.out.println("你出拳: " + choice[myChoice-1]);
            System.out.println(computerRole + "出拳: " + choice[computerChoice-1]);

            determineWinner(myChoice, computerChoice);
        }


    }

    // 判断胜负
    public static void determineWinner(int myChoice, int computerChoice) {
        if (myChoice == computerChoice) {
            equalCount++;
            System.out.println("平局，加油啊!");
        } else if ((myChoice == 1 && computerChoice == 2) ||
                (myChoice == 2 && computerChoice == 3) ||
                (myChoice == 3 && computerChoice == 1)) {
            System.out.println("耶！我赢了！");
            myScore++;
        } else {
            System.out.println("啊！你输了！");
            computerScore++;
        }
    }


    // 询问是否继续
    public static void askStart() {
        System.out.print(isContinue);
        String answer = sc.next();

        if (answer.equals("y")) {
            gameRunning = true;
            playCount++;
            isContinue = "是否开始下一轮? (y/n)";
        }else if (answer.equals("n")) {
            gameRunning = false;
        }else {
            System.out.println("输入无效，请输入y/n");
            askStart();
        }
    }

    //显示退出消息
    public static void showExitMessage() {
        System.out.println("----------------------------------");
        System.out.println("\t\t"+myRole + " 对决 " + computerRole);

        System.out.println("次数："+playCount+"次");
        System.out.println("平局："+equalCount+"次");
        System.out.println(myRole+"得："+myScore+"分");
        System.out.println(computerRole+"得："+computerScore+"分");

        if (myScore > computerScore) {
            System.out.println("最终结果："+myRole+"赢！");
            System.out.println("----------------------------------");
        } else if (myScore < computerScore) {
            System.out.println("最终结果："+computerRole+"赢！");
            System.out.println("----------------------------------");
        } else {
            System.out.println("最终结果：平局!");
            System.out.println("----------------------------------");
        }
    }


}

